<template>
	<view class="cart-container" v-if="cart.length !== 0">
		<my-address></my-address>
		<!-- 商品列表的标题区域 -->
		<view class="cart-title">
			<!-- 左侧的图标 -->
			<uni-icons type="shop" size="30"></uni-icons>
			<!-- 右侧的文本 -->
			<text class="cart-title-text">购物车</text>
		</view>
		<!-- 商品列表区域 -->
		<uni-swipe-action>
			<block v-for="(goods,i) in cart" :key="i">
			<uni-swipe-action-item :right-options="options" @click="swipeItemHandler(goods)">
				<my-goods :goods="goods" :showRadio="true" :showNum="true" @radio-change="radioChangeHandler" @num-change="numberChangeHandler"></my-goods>
			</uni-swipe-action-item>
			</block>
		</uni-swipe-action>
		
		<!-- 使用自定义组件（结算） -->
		<my-settle></my-settle>
	</view>
	
	<!-- 空白区域购物车 -->
	<view class="none" v-else>
		<image src="../../static/cart_empty@2x.png" mode=""></image>
		<text>快去选购吧</text>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js' 
	import { mapState ,mapMutations} from 'vuex'
	export default {
		mixins:[badgeMix],
		computed:{
			...mapState('m_cart',['cart']),
		},
		data() {
			return {
				options:[{
					text:'删除',
					style:{
						backgroundColor:'#c00000'
					}
				}]
			};
		},
		methods:{
			...mapMutations('m_cart',['updateGoodsState','updateGoodsCount','removeGoodById']),
			//radio 组件的 change 事件处理函数
			radioChangeHandler(e){
				console.log(e)
				this.updateGoodsState(e)
			},
			//NumberBox 组件的 change 事件处理函数
			numberChangeHandler(e){
				console.log(e)
				this.updateGoodsCount(e)
			},
			//左划删除规定id的商品
			swipeItemHandler(goods){
				this.removeGoodById(goods.goods_id)
			}
		}
	}
</script>

<style lang="scss">
	.cart-container{
		padding-bottom: 100rpx;
	}
.cart-title{
	height: 80rpx;
	display: flex;
	align-items: center;
	padding-left: 10rpx;
	border-bottom: 1rpx solid #efefef;
	.cart-title-text{
		margin-left: 30rpx;
	}
}
.none{
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-top: 350rpx;
}
.none image{
	width: 250rpx;
	height: 250rpx;
}
.none text{
	font-size: 12px;
	    color: gray;
	    margin-top: 15px;
}
</style>
